package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.equipmentCheck.domain.EquipmentCheck;
import com.sjgtw.cloud.web.equipmentCheck.service.IEquipmentCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 防洪设备检查Controller
 * 
 * @author lvyoufang
 * @date 2024-03-09
 */
@RestController
@RequestMapping("/app/equipmentCheck")
@Api(tags = "6.3.1、APP应急设施-防洪设备检查")
public class AppEquipmentCheckController extends BaseController
{
    @Autowired
    private IEquipmentCheckService equipmentCheckService;

    @Autowired
    private SystemServerService systemServerService;

    @Autowired
    private RedisService redisService;

    /**
     * 查询防洪设备检查列表
     */
    @ApiOperation(value = "查询防洪设备检查列表")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentCheck equipmentCheck)
    {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        equipmentCheck.setCreateUserId(user.getUserid());
      /*  if (user != null) {
            AjaxResult ajaxResult = systemServerService.getUserDeptLevel(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
                    String dept = jsonObject.getString("dept");
                    Long deptId= jsonObject.getLong("deptId");
                    Long parentId= jsonObject.getLong("parentId");
                    if("section".equals(dept)){
                        equipmentCheck.setSection(deptId);
                    }else if("workArea".equals(dept)){
                        equipmentCheck.setSection(parentId);
                        equipmentCheck.setWorkArea(deptId);
                    }
                }

            }
        }
*/

        startPage();
        List<EquipmentCheck> list = equipmentCheckService.selectEquipmentCheckList(equipmentCheck);
        return getDataTable(list);
    }

    /**
     * 获取防洪设备检查详细信息
     */
    @ApiOperation(value = "获取防洪设备检查详细信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(equipmentCheckService.selectEquipmentCheckById(id));
    }

    /**
     * 新增防洪设备检查
     */
    @ApiOperation(value = "新增防洪设备检查")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EquipmentCheck equipmentCheck)
    {

        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }
        equipmentCheck.setCreateUserId(user.getUserid());
        /*if (user != null) {
            AjaxResult ajaxResult = systemServerService.getUserDeptLevel(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
                    String dept = jsonObject.getString("dept");
                    Long deptId= jsonObject.getLong("deptId");
                    Long parentId= jsonObject.getLong("parentId");
                    if("section".equals(dept)){
                        equipmentCheck.setSection(deptId);
                    }else if("workArea".equals(dept)){
                        equipmentCheck.setSection(parentId);
                        equipmentCheck.setWorkArea(deptId);
                    }
                }

            }
        }
*/

        return toAjax(equipmentCheckService.insertEquipmentCheck(equipmentCheck));
    }

    /**
     * 修改防洪设备检查
     */
    @ApiOperation(value = "修改防洪设备检查")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody EquipmentCheck equipmentCheck)
    {
        return toAjax(equipmentCheckService.updateEquipmentCheck(equipmentCheck));
    }

    /**
     * 删除防洪设备检查
     */
    @ApiOperation(value = "删除防洪设备检查")
    @PostMapping("/remove")
    public AjaxResult remove(String[] ids) {
        return toAjax(equipmentCheckService.deleteEquipmentCheckByIds(ids));
    }
}
